package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;

import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public void add(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());

        courseMapper.insert(course);
    }

    @Override
    public void deleteById(Integer id) {
       courseMapper.deleteById(id);
    }

    @Override
    public void update(Course course) {
        courseMapper.update(course);
    }


    @Override
    public Course getById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public PageResult<Course> list(String name, Integer subject, Integer price, Integer target, String description, Integer page, Integer pageSize) {
        //设置PageHelper分页参数
        PageHelper.startPage(page, pageSize);

        //调用mapper接口方法,执行查询
        List<Course> courseList = courseMapper.selectList(name, subject, price, target, description);

        //封装并封装分页结果
        Page<Course> p = (Page<Course>) courseList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public List<Course> selectAll() {
        return courseMapper.selectAll();
    }

    @Override
    public List<Course> listBySubject(Integer subject) {
        return courseMapper.listBySubject(subject);

    }
}
